function adminUpdateCheck()
{
	ajax.sendAsync("./action/admin/update.php5?action=check&u="+new Date().getTime(), function(datas)
	{
		ajax.eval(datas)
	})
}

var update_popup = null
var update_canceled = false
function adminUpdateSave()
{
	var update_form = document.getElementById("update_form");
	update_canceled = false;
	disabledForm(update_form, true)
	ajax.sendAsync("./action/admin/update.php5?action=perform&u="+new Date().getTime(), function(datas)
	{
		ajax.eval(datas);
	}, ajax.buildDatas(update_form))
}

function updatePerform(id, head, status)
{
	if(update_canceled) return
	if(head!=null)
	{
		document.getElementById("update_head").innerHTML = head
	}
	if(status!=null)
	{
		document.getElementById("update_status").innerHTML = status
	}
	ajax.sendAsync("./action/admin/update.php5?action=install&id="+id+"&u="+new Date().getTime(), function(datas)
	{
		ajax.eval(datas);
	})
}

function updateStartProgress()
{
	if(update_progressing) return
	update_progressing = true;
	last_updateProgressReponse = null;
	updateProgressDisplay();
}
function updateStopProgress()
{
	update_progressing = false;
	clearTimeout(to_update)
	last_updateProgressReponse = null;
	document.getElementById("update_progressbar").style.width = "0%"
}

var to_update=0
var update_progressing = false;
var updateProgressReponse=[0, '']
var last_updateProgressReponse = null;
function updateProgressDisplay()
{
	if(update_canceled) return
	ajax.sendAsync("./action/admin/update.php5?action=progress&u="+new Date().getTime(), function(datas)
	{
		if(update_canceled) return
		if(!update_progressing) return
		if(last_updateProgressReponse==null)
		{
			last_updateProgressReponse = updateProgressReponse[0]
			last_updateProgressReponseChange = new Date();
		}
		
		ajax.eval(datas);
		
		if(last_updateProgressReponse!=updateProgressReponse[0])
		{
			last_updateProgressReponse = updateProgressReponse[0]
			last_updateProgressReponseChange = new Date();
		}
		if(new Date().getTime() - last_updateProgressReponseChange.getTime() > 60*1000)
		{
			return;
		}
		
		document.getElementById("update_progressbar").style.width = updateProgressReponse[0]+"%"
		if(updateProgressReponse[1]!='')
		{
			document.getElementById("update_status").innerHTML = updateProgressReponse[1]
		}
		to_update = setTimeout(updateProgressDisplay, 1000);
	})
}

function updateCancel()
{
	update_canceled = true;
	var btn_cancel = document.getElementById("update_btn_cancel");
	btn_cancel.setAttribute('disabled', '1')
	ajax.sendAsync("./action/admin/update.php5?action=cancel&u="+new Date().getTime(), function(datas)
	{
		ajax.eval(datas);
	})

}

function updateDisplayError(message)
{
	if(update_canceled) return;
	document.getElementById("update_status_icon").className = 'error popup_feet_message'
	document.getElementById("update_status").innerHTML = message
}

var update_versions = new Array();
var update_dico = new Array();
function updateDisplayVersions(versions)
{
	update_versions = versions; 
	var ch = new Array();
	for(var i=0; i<versions.length; i++)
	{
		ch.push('<div id="install_'+versions[i]+'" class="install_version"> \
<label>'+versions[i].replace('___', ' ')+'</label>\
<ul>\
<li class="install_step_download"><div class="tooltips"><div></div><span class="tip"><div class="tip_body"><nobr>'+update_dico['download']+'</nobr></div></span></div></li>\
<li class="install_step_check"><div class="tooltips"><div></div><span class="tip"><div class="tip_body"><nobr>'+update_dico['check']+'</nobr></div></span></div></li>\
<li class="install_step_requirement"><div class="tooltips"><div></div><span class="tip"><div class="tip_body"><nobr>'+update_dico['requirement']+'</nobr></div></span></div></li>\
<li class="install_step_extract"><div class="tooltips"><div></div><span class="tip"><div class="tip_body"><nobr>'+update_dico['extract']+'</nobr></div></span></div></li>\
<li class="install_step_sql"><div class="tooltips"><div></div><span class="tip"><div class="tip_body"><nobr>'+update_dico['sql']+'</nobr></div></span></div></li>\
<li class="install_step_script"><div class="tooltips"><div></div><span class="tip"><div class="tip_body"><nobr>'+update_dico['scripts']+'</nobr></div></span></div></li>\
</ul>\
</div>');
	}
	document.getElementById("update_versions").innerHTML = ch.join("")
}


function updateUpdateStepCurrent(application, version, step)
{
	if(version == null)
	{
		if(step==null)
		{
			for(var i=0; i<update_versions.length; i++)
			{
				if(!document.getElementById("install_"+update_versions[i])) return;
				document.getElementById("install_"+update_versions[i]).removeAttribute("current")
				document.getElementById("install_"+update_versions[i]).className+=""
			}
		}
		else
		{
			for(var i=0; i<update_versions.length; i++)
			{
				if(!document.getElementById("install_"+update_versions[i])) return;
				document.getElementById("install_"+update_versions[i]).setAttribute("current", step);
				document.getElementById("install_"+update_versions[i]).className+=""
			}
		}
	}
	else if(step==null)
	{
		if(!document.getElementById("install_"+application+"___"+version)) return;
		document.getElementById("install_"+application+"___"+version).removeAttribute("current");
		document.getElementById("install_"+application+"___"+version).className+=""
	}
	else
	{
		if(!document.getElementById("install_"+application+"___"+version)) return;
		document.getElementById("install_"+application+"___"+version).setAttribute("current", step);
		document.getElementById("install_"+application+"___"+version).className+=""
		updateUpdateStepDone(application, version, step-1)
	}
}

function updateUpdateStepDone(application, version, step)
{
	if(version == null)
	{
		for(var i=0; i<update_versions.length; i++)
		{
			if(!document.getElementById("install_"+update_versions[i])) return;
			document.getElementById("install_"+update_versions[i]).setAttribute("done", step);
			document.getElementById("install_"+update_versions[i]).className+=""
		}
	}
	else
	{
		if(!document.getElementById("install_"+application+"___"+version)) return;
		document.getElementById("install_"+application+"___"+version).setAttribute("done", step);
		document.getElementById("install_"+application+"___"+version).className+=""
	}
}